package com.chenxingnet.service.Impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chenxingnet.dao.RemindMapper;
import com.chenxingnet.dao.WaybillsMapper;
import com.chenxingnet.dto.Remind;
import com.chenxingnet.dto.Waybills;
import com.chenxingnet.service.IRemindService;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;
@Service("remindService")
public class RemindServiceImpl    implements   IRemindService{
	
	@Resource
	RemindMapper  remindMapper;
	@Resource
	WaybillsMapper waybillsMapper;
	
	// 由IGetui管理页面生成，是您的应用与SDK通信的标识之一，每个应用都对应一个唯一的AppID  
    private static String appId = "gNYsEJvX3r78FKn1wjmgi3";  
    // 预先分配的第三方应用对应的Key，是您的应用与SDK通信的标识之一。  
    private static String appKey = "GHNVVDUnH28JLVapYKEuS2";  
    // 个推服务端API鉴权码，用于验证调用方合法性。在调用个推服务端API时需要提供。（请妥善保管，避免通道被盗用）  
    private static String masterSecret = "XjoXHOZSxW72Kfl936gfX9";
    private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
    //研究个推
    
    /**
     * 根据新增运单生成提醒信息
     * 进行推送至手机端
     */
    @SuppressWarnings("deprecation")
	public int getuiRemindByBills(Waybills waybills) {
		int  res=0;
		Remind   remind2=new  Remind();
		remind2.setRpubdate(new  Date());
		//判断是否已看过，看过后显示为0
		remind2.setRreportstart("1");
		remind2.setWid(waybills.getWid());
		remind2.setType("到达时间提醒");
		//是否显示此信息，如运单已送达或者发生事故，值为0，不显示此信息
		remind2.setRsend("1");
		String   context2=waybills.getWbourn() +"_"+waybills.getWvin()+" ,将要到期。";	
		remind2.setRcontent(context2);
		res=	remindMapper.insertSelective(remind2);	//插入一条临时牌提醒
		
			 NotificationTemplate template = new NotificationTemplate();
		        // 设置APPID与APPKEY
		        template.setAppId(appId);
		        template.setAppkey(appKey);
		        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
		        template.setTransmissionType(1);
		        template.setTitle("超时提醒");
		        template.setText(context2);
		        template.setIsRing(true);
		        template.setIsClearable(true);
		        template.setIsVibrate(true);
		        
		        IGtPush push = new IGtPush(url, appKey, masterSecret);
		        List<String> appIds = new ArrayList<String>();
		        appIds.add(appId);

		        // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
		        AppMessage message = new AppMessage();
		        message.setData(template);
		        message.setAppIdList(appIds);
		        message.setOffline(true);
		        message.setOfflineExpireTime(1000 * 600);

		        IPushResult ret = push.pushMessageToApp(message);
		        System.out.println(ret.getResponse().toString());
		        
		      
		return res;
	}
    
    
    
    /**
     * 根据新增运单生成提醒信息
     * 弃用
     */
	public int generateRemindByBills(Waybills waybills) {
		int  res=0;
		Remind   remind2=new  Remind();
		remind2.setRpubdate(new  Date());
		remind2.setRreportstart("1");
		remind2.setWid(waybills.getWid());
		remind2.setType("到达时间提醒");
		String   context2=waybills.getWbourn() +"_"+waybills.getWvin()+" ,将要到期。";	
		remind2.setRcontent(context2);
		res=	remindMapper.insertSelective(remind2);	//插入一条临时牌提醒
		return res;
	}

	public List<Remind> getRemindsBydepid(int deptid) {
		String song = "未送达";
		Waybills waybills = new Waybills();
		waybills.setDeptid(deptid);
		waybills.setWdelivery(song);
		return remindMapper.getRemindsBydepid(waybills);
	}
	//获取全部提醒信息
	public List<Remind> getAllReminds() {
		List<Remind> rem = new ArrayList<Remind>();
		List<Waybills> list = waybillsMapper.selectByDeli();
		
		for (int i = 0; i < list.size(); i++) {
			Remind remind = remindMapper.getRemByWid(list.get(i).getWid());
			if(remind != null){
				rem.add(remind);
			}
		}
		
		return rem;
	}
	
	public int updateByWid(Remind remind) {
		
		return remindMapper.updateByWid(remind);
	}

	
	public void getDaySub(){
		//小写的mm表示的是分钟HH:mm:ss
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		int day = 0;
		//获取未送达运单
		List<Waybills> list = waybillsMapper.getDayShowWay();
		for (int i = 0; i < list.size(); i++) {
			long time = list.get(i).getWestimatedtime().getTime();
			//获取预计到达时间减去当前时间
			day = (int)((list.get(i).getWestimatedtime().getTime())-(new Date().getTime()));
			if(day < 24*60*60*1000 && day >= 0){
				//判断是否重复
				Remind rem = remindMapper.getDayByWid(list.get(i).getWid());
				if(rem == null){
					Waybills waybills = new Waybills();
					waybills.setWid(list.get(i).getWid());
					waybills.setWbourn(list.get(i).getWbourn());
					waybills.setWvin(list.get(i).getWvin());
					int bills = getuiRemindByBills(waybills);
				}
			}
			
		}
		
		
		
	}
	
}
